package com.ysg.widget.dialog.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ysg.utils.YRecyclerViewUtil;

import java.util.List;

public  abstract class GridPageAdapter<T> extends PagerAdapter {
    private Context context;
    private List<List<T>> list;
    private int count = 3;

    public GridPageAdapter(Context context) {
        this.context = context;
    }
    public GridPageAdapter(Context context, int count) {
        this.context = context;
        this.count = count;
    }

    public void setData(List<List<T>> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
        return view == o;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        RecyclerView recyclerView = new RecyclerView(context);

        BaseQuickAdapter adapter = getChildAdapter();
        if (adapter != null){
            adapter.setList(list.get(position));
            YRecyclerViewUtil.initGrid(context, recyclerView, adapter, count);
        }

        container.addView(recyclerView);
        return recyclerView;
    }

    public abstract BaseQuickAdapter getChildAdapter();



}
